package cn.liutao.auth.api;

import cn.hutool.core.lang.UUID;
import cn.liutao.auth.service.AuthService;
import cn.liutao.common.enums.CaptchaEnum;
import cn.liutao.common.enums.CommonEnum;
import cn.liutao.common.utils.RedisUtils;
import cn.liutao.common.utils.ServletUtils;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author liutao
 * @Description 获取验证码接口
 * @date 2023/10/30 19:35
 * @Version 1.0
 */
@WebServlet("/auth/captcha")
@Slf4j
public class CaptchaServlet extends HttpServlet {

    private AuthService authService;
    /**
     * 初始化
     * @param config
     * @throws ServletException
     */
    @Override
    public void init(ServletConfig config) throws ServletException {
        authService = AuthService.getInstance();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp){
        String key = UUID.randomUUID().toString();
        Map<String,Object> map = authService.genCaptcha(CaptchaEnum.HUTOOL_CAPTCHA_TYPE_LINE.getValue(), 140, 40, 4, 10);
        String code = (String) map.get("key");
        //将验证码存储到redis域中
        RedisUtils.set(CommonEnum.CAPTCHA_KEY.getValue()+key,code,60*10L);
        log.info("验证码：【{}】", code);
        map.put("key", key);
        ServletUtils.ok(resp,map);

    }
}
